package com.eudon.design_pattern.behavioral.interpreter;

/**
 * @author eudon
 * @description 告警系统，使用解释器判断是否需要告警
 * @date 2025/9/3
 * @email syd19970616@gmail.com
 * @address <a href="https://gitee.com/eudon">Gitee</a>
 */
public class AlertSystem {
    
    /**
     * 检查告警规则
     * @param ruleString 规则字符串
     * @param context 上下文数据
     * @return 是否需要告警
     */
    public boolean checkAlert(String ruleString, Context context) {
        try {
            // 解析规则字符串
            RuleExpression rule = RuleParser.parse(ruleString);
            
            // 执行规则判断
            boolean shouldAlert = rule.interpret(context);
            
            // 输出判断结果
            System.out.println("规则: " + ruleString);
            System.out.println("解析后: " + rule.toString());
            System.out.println("判断结果: " + (shouldAlert ? "需要告警" : "正常"));
            
            // 如果需要告警，发送告警
            if (shouldAlert) {
                sendAlert(ruleString, context);
            }
            
            return shouldAlert;
            
        } catch (Exception e) {
            System.err.println("规则解析失败: " + e.getMessage());
            return false;
        }
    }
    
    /**
     * 发送告警
     */
    private void sendAlert(String ruleString, Context context) {
        System.out.println("🚨 告警触发!");
        System.out.println("告警规则: " + ruleString);
        System.out.println("告警时间: " + java.time.LocalDateTime.now());
        System.out.println("建议: 请及时检查相关接口的运行状态");
        System.out.println("----------------------------------------");
    }
}